package com.example.jf.jfapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jf.jfapp.adapter.MainAdapter;
import com.example.jf.jfapp.fragment.ShouyeFragment;
import com.example.jf.jfapp.fragment.WodeFragment;
import com.example.jf.jfapp.fragment.XuexiFragment;
import com.example.jf.jfapp.fragment.faxian.FaxianFragment;
import com.example.jf.jfapp.widget.bottomtab.BottomLayout;
import com.example.jf.jfapp.widget.bottomtab.TabBean;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.ll_ab)
    LinearLayout ll_ab;

    ArrayList<TabBean> tabBeens = new ArrayList<>();
    @BindView(R.id.publish)
    ImageView publish;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Fresco.initialize(this);
        ZXingLibrary.initDisplayOpinion(this);
        //初始化提交
        //测试
        //哎
        ButterKnife.bind(this);
        BottomLayout bl = (BottomLayout) findViewById(R.id.bl);

        ViewPager vp = (ViewPager) findViewById(R.id.vp);

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new ShouyeFragment());
        fragments.add(new XuexiFragment());
        fragments.add(new FaxianFragment());
        fragments.add(new WodeFragment());


        tabBeens.add(new TabBean("首页", R.drawable.ic_account_balance_selected, R.drawable.ic_account_balance_normal));
        tabBeens.add(new TabBean("学习", R.drawable.ic_chat_bubble_outline_selected, R.drawable.ic_chat_bubble_outline_normal));
        tabBeens.add(new TabBean("发现", R.drawable.ic_explore_selected, R.drawable.ic_explore_normal));
        tabBeens.add(new TabBean("我的", R.drawable.ic_account_box_selected, R.drawable.ic_account_box_normal));

        final MainAdapter mainAdapter = new MainAdapter(getSupportFragmentManager(), fragments);
        vp.setAdapter(mainAdapter);
        bl.setViewPager(vp, tabBeens);
        //设置VP的缓存数量，我们不用懒加载，这样比较省事
        vp.setOffscreenPageLimit(tabBeens.size());


//这个代码是用来设置position=1的时候，toolbar隐藏
        BottomLayout.BottomPageChangeListener bottomPageChangeListener = new BottomLayout.BottomPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
//                if(position == 1){
//                    ll_ab.setVisibility(View.GONE);
//                    return;
//                }else {
                tv_title.setText(tabBeens.get(position).getTitle());
                ll_ab.setVisibility(View.VISIBLE);
                //             }
                if (position == 2) {
                    publish.setVisibility(View.VISIBLE);
publish.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent publishIntent=new Intent(MainActivity.this,PublishActivity.class);
            startActivity(publishIntent);

    }
});

                }else {
                    //重置发布动态的可见度
                    publish.setVisibility(View.GONE);
                }
            }
        };

        bl.setBottomPageChangeListener(bottomPageChangeListener);


    }

}
